package 力扣日常刷题.木22二月.第11天217;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/2/17
 * @description
 */
public class Demo80两数相加 {


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int c = 0;
        ListNode h = null, t = null;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + c;
            if (h == null) {
                h = t = new ListNode(sum % 10);
            } else {
                t.next = new ListNode(sum % 10);
                t = t.next;
            }
            c = sum / 10;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if(c > 0) t.next = new ListNode(c);
        return h;

    }
}
